package com.bj58.groupbuy.action.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import com.bj58.groupbuy.action.model.ClickCityVo;
import com.bj58.groupbuy.action.model.ClickEventsSourceVo;
import com.bj58.groupbuy.action.model.ConflictDelectVo;
import com.bj58.groupbuy.action.model.TrackURLToPVUVVo;
import com.bj58.groupbuy.action.model.UrlToPvUvVo;
import com.bj58.groupbuy.action.service.StatisticsPlanService;
import com.bj58.groupbuy.action.util.HbaseUtil;

public class StatisticsPlanServiceImpl implements StatisticsPlanService {
	@Override
	public List<String> generateRowKeyPrefixListForBi(List<String> dateList,
			String eventKey, String platForm) throws Exception {
		// TODO Auto-generated method stub
		
		List <String> resultList=new ArrayList<String>(); //获取页面日期范围
		Map<String, String> rowKey = new TreeMap<String, String>();
		for(String dateString:dateList){
					rowKey.put("00", dateString);
					rowKey.put("01", platForm);
					rowKey.put("02", eventKey);
			resultList.add(HbaseUtil.RowKeyToString((TreeMap<String, String>) rowKey));
		} 
	    return resultList;		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Map<String, Object>> QueryForListByPrefix(
			List<String> rowKeyList, String tableName,String statdate) throws Exception {
		// TODO Auto-generated method stub
	    	Map <String,Map<String,Object>> resultMap=new HashMap<String,Map<String,Object>>();
	    	Map<String, Map<String,Object>> hBaseResult=new HashMap<String,Map<String,Object>>();
	    	String rowKey;
			String newKey;
			String newKeyTmp;
	    	for(String rowKeyPrefix:rowKeyList){
	    		hBaseResult = HbaseUtil.getDataByKeyPrefix(tableName,rowKeyPrefix);
				for (Entry<?, ?> entry : hBaseResult.entrySet()) {
	    			Map<String,Object> hBaseMap=(Map<String,Object>)entry.getValue();
	        		 rowKey=String.valueOf(entry.getKey());
	        		 String sdate=rowKey.substring(0, 8);
	        		 newKeyTmp=rowKey.substring(rowKey.indexOf("_", 0)+1);
	        		 newKey=newKeyTmp.substring(newKeyTmp.indexOf("_",0)+1);
        		 if("".equals(statdate))
                 {
	        		if(resultMap.containsKey(newKey)){
	        		   Map<String,Object> mapElement=resultMap.get(newKey);
						for (Entry<?, ?> entryElement : hBaseMap.entrySet()) {
	        			   String key=String.valueOf(entryElement.getKey());
	        			   if  (     null !=hBaseMap.get(key)
	        					     &&  key.contains("cf:")
	        					     && !"cf:atDate".equals(key)
	        					     && !String.valueOf(entryElement.getValue()).equals("")
	        					){
	        				       if(mapElement.get(key)==null && !String.valueOf(entryElement.getValue()).equals("") ){
	        				    	   mapElement.put(key, String.valueOf(entryElement.getValue())); 
	        				       }else{
	        				    	   long val1=Long.parseLong(String.valueOf(mapElement.get(key)).equals("")?"0":String.valueOf(mapElement.get(key)));
	        				    	   int  val2=Integer.parseInt(String.valueOf(entryElement.getValue()));
	        				    	   long val=val1+val2;
	        				    	   mapElement.put(key, val);
	        				       }
	        				       resultMap.put(newKey, mapElement);
	        			   }
	        		   }
	        		}else{
	        		   Map<String, Object> tmpMap=(Map<String, Object>) entry.getValue();
	        		   tmpMap.put("rowKey", newKey);
	        		   resultMap.put(newKey,tmpMap); 
	        		}
                 }else {
					
                	 if(resultMap.containsKey(rowKey)){
  	        		   Map<String,Object> mapElement=resultMap.get(rowKey);
  						for (Entry<?, ?> entryElement : hBaseMap.entrySet()) {
  	        			   String key=String.valueOf(entryElement.getKey());
  	        			   if  (     null !=hBaseMap.get(key)
  	        					     &&  key.contains("cf:")
  	        					     && !"cf:atDate".equals(key)
  	        					     && !String.valueOf(entryElement.getValue()).equals("")
  	        					){
  	        				       if(mapElement.get(key)==null && !String.valueOf(entryElement.getValue()).equals("") ){
  	        				    	   mapElement.put(key, String.valueOf(entryElement.getValue())); 
  	        				       }else{
  	        				    	   long val1=Long.parseLong(String.valueOf(mapElement.get(key)).equals("")?"0":String.valueOf(mapElement.get(key)));
  	        				    	   int  val2=Integer.parseInt(String.valueOf(entryElement.getValue()));
  	        				    	   long val=val1+val2;
  	        				    	   mapElement.put(key, val);
  	        				       }
  	        				       resultMap.put(rowKey, mapElement);
  	        			   }
  	        		   }
  	        		}else{
  	        		   Map<String, Object> tmpMap=(Map<String, Object>) entry.getValue();
  	        		   tmpMap.put("rowKey", newKey);
  	        		   tmpMap.put("sdate", sdate);
  	        		   resultMap.put(rowKey,tmpMap); 
  	        		}
                	 
                	 
				}
        		 
	        	}
	    		
	    	}
	    	return resultMap;
			
		
	}

	@Override
	public Map<String, Map<String, Object>> QueryForListByKey(
			List<String> rowKeyList, String tableName,String statdate) throws Exception {
		// TODO Auto-generated method stub
		Map <String,Map<String,Object>> resultMap=new HashMap<String,Map<String,Object>>();
    	//Map<String, Map<String,Object>> hBaseResult=new HashMap<String,Map<String,Object>>();
    	String rowKey;
		String newKey;
		String newKeyTmp;
    	for(String rowKeyPrefix:rowKeyList){
    		Map<String,Object> tmpResult = HbaseUtil.getDataByRowkey(tableName,rowKeyPrefix);
    		Map<String, Map<String,Object>> hBaseResult=new HashMap<String,Map<String,Object>>();
    		hBaseResult.put(rowKeyPrefix, tmpResult);
			for (Entry<?, ?> entry : hBaseResult.entrySet()) {
    			@SuppressWarnings("unchecked")
				Map<String,Object> hBaseMap=(Map<String,Object>)entry.getValue();
        		 rowKey=String.valueOf(entry.getKey());
        		 String sdate=rowKey.substring(0, 8);
        		 newKeyTmp=rowKey.substring(rowKey.indexOf("_", 0)+1);
        		 newKey=newKeyTmp.substring(newKeyTmp.indexOf("_",0)+1);
    		 if("".equals(statdate))
             {
        		if(resultMap.containsKey(newKey)){
        		   Map<String,Object> mapElement=resultMap.get(newKey);
					for (Entry<?, ?> entryElement : hBaseMap.entrySet()) {
        			   String key=String.valueOf(entryElement.getKey());
        			   if  (     null !=hBaseMap.get(key)
        					     &&  key.contains("cf:")
        					     && !"cf:atDate".equals(key)
        					     && !String.valueOf(entryElement.getValue()).equals("")
        					){
        				       if(mapElement.get(key)==null && !String.valueOf(entryElement.getValue()).equals("") ){
        				    	   mapElement.put(key, String.valueOf(entryElement.getValue())); 
        				       }else{
        				    	   long val1=Long.parseLong(String.valueOf(mapElement.get(key)).equals("")?"0":String.valueOf(mapElement.get(key)));
        				    	   int  val2=Integer.parseInt(String.valueOf(entryElement.getValue()));
        				    	   long val=val1+val2;
        				    	   mapElement.put(key, val);
        				       }
        				       resultMap.put(newKey, mapElement);
        			   }
        		   }
        		}else{
        		   @SuppressWarnings("unchecked")
				Map<String, Object> tmpMap=(Map<String, Object>) entry.getValue();
        		   tmpMap.put("rowKey", newKey);
        		   resultMap.put(newKey,tmpMap); 
        		}
             }else {
            	 if(resultMap.containsKey(rowKey)){
          		   Map<String,Object> mapElement=resultMap.get(rowKey);
  					for (Entry<?, ?> entryElement : hBaseMap.entrySet()) {
          			   String key=String.valueOf(entryElement.getKey());
          			   if  (     null !=hBaseMap.get(key)
          					     &&  key.contains("cf:")
          					     && !"cf:atDate".equals(key)
          					     && !String.valueOf(entryElement.getValue()).equals("")
          					){
          				       if(mapElement.get(key)==null && !String.valueOf(entryElement.getValue()).equals("") ){
          				    	   mapElement.put(key, String.valueOf(entryElement.getValue())); 
          				       }else{
          				    	   long val1=Long.parseLong(String.valueOf(mapElement.get(key)).equals("")?"0":String.valueOf(mapElement.get(key)));
          				    	   int  val2=Integer.parseInt(String.valueOf(entryElement.getValue()));
          				    	   long val=val1+val2;
          				    	   mapElement.put(key, val);
          				       }
          				       resultMap.put(rowKey, mapElement);
          			   }
          		   }
          		}else{
          		   @SuppressWarnings("unchecked")
  				Map<String, Object> tmpMap=(Map<String, Object>) entry.getValue();
          		   tmpMap.put("rowKey", newKey);
          		   tmpMap.put("sdate", sdate);
          		   resultMap.put(rowKey,tmpMap); 
          		}
			}
        	}
    		
    	}
    	return resultMap;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UrlToPvUvVo> DataInJson(Map<String, Map<String, Object>> dataMap)
			throws IOException {
		// TODO Auto-generated method stub
		List<UrlToPvUvVo> resultlist = new ArrayList<UrlToPvUvVo>();
		boolean statflag=false;
		if (dataMap != null && !dataMap.isEmpty()) {
			List<Map<String, Object>> rsList = ConvertMapToList(dataMap);
			for (int i = 0; i < rsList.size(); i++) {
				String[] tempString=String.valueOf(rsList.get(i)).replace("}", "").split(",");
				UrlToPvUvVo tempPvUvVo=new UrlToPvUvVo();
				if(tempString.length>=3)
				{
					tempPvUvVo.setUrlsource(tempString[0].split("=")[1]);
					if(tempString.length>=4)
					{
						tempPvUvVo.setStatdate(tempString[1].split("=")[1]);
						tempPvUvVo.setUrl_pv(tempString[2].split("=")[1]);
						tempPvUvVo.setUrl_uv(tempString[3].split("=")[1]);
						statflag=true;
					}else {
						tempPvUvVo.setUrl_pv(tempString[1].split("=")[1]);
						tempPvUvVo.setUrl_uv(tempString[2].split("=")[1]);
					}
					resultlist.add(tempPvUvVo);
				}
			}
		}
		if(statflag)
		{
			Collections.sort(resultlist);  
		}
		return resultlist;
	}

	 public static List<Map<String, Object>> ConvertMapToList(Map<String,Map<String,Object>> dataMap){
		  List<Map<String, Object>> resultList=new ArrayList<Map<String,Object>>();
			for (Entry<?, ?> entry : dataMap.entrySet()) {
			  @SuppressWarnings("unchecked")
			Map<String, Object> map=(Map<String, Object>) entry.getValue();
			  resultList.add(map);
			  
		  }
		  return resultList;
		  
	  }

	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Map<String, Object>> QueryForListByKeyForTrack(
			List<String> rowKeyList, String tableName,String statdate) throws Exception {
		// TODO Auto-generated method stub
		Map <String,Map<String,Object>> resultMap=new HashMap<String,Map<String,Object>>();
    	//Map<String, Map<String,Object>> hBaseResult=new HashMap<String,Map<String,Object>>();
    	String rowKey;
		String newKey;
		String newKeyTmp;
    	for(String rowKeyPrefix:rowKeyList){
    		Map<String,Object> tmpResult = HbaseUtil.getDataByRowkey(tableName,rowKeyPrefix);
    		Map<String, Map<String,Object>> hBaseResult=new HashMap<String,Map<String,Object>>();
    		hBaseResult.put(rowKeyPrefix, tmpResult);
    		for(Entry entry:hBaseResult.entrySet()){
    			@SuppressWarnings("unchecked")
				Map<String,Object> hBaseMap=(Map<String,Object>)entry.getValue();
        		 rowKey=String.valueOf(entry.getKey());
        		 String sdate=rowKey.substring(0, 8);
        		 newKeyTmp=rowKey.substring(rowKey.indexOf("_", 0)+1);
        		 newKey=newKeyTmp.substring(newKeyTmp.indexOf("_",0)+1);
        		 String [] arrTmp = newKey.split("_");
        		 if(arrTmp.length < 5)
        		 {
        			 continue;
        		 }
        		 String pagetype = (arrTmp[0] == null || arrTmp[0].equals("")) ? "":arrTmp[0];
        		 String page = (arrTmp[1] == null || arrTmp[1].equals("")) ? "":arrTmp[1];
        		 String area = (arrTmp[2] == null || arrTmp[2].equals("")) ? "":arrTmp[2];
        		 String cate1 = (arrTmp[3] == null || arrTmp[3].equals("")) ? "":arrTmp[3];
        		 String cate2 = (arrTmp[4] == null || arrTmp[4].equals("")) ? "":arrTmp[4];
        		 hBaseMap.put("cf:pagetype", pagetype);
        		 hBaseMap.put("cf:page", page);
        		 hBaseMap.put("cf:area", area);
        		 hBaseMap.put("cf:cate1", cate1);
        		 hBaseMap.put("cf:cate2", cate2);
               
                 if("".equals(statdate))
                 {
	        		 if(resultMap.containsKey(newKey))
	        		 {
	        			 Map<String,Object> mapElement = resultMap.get(newKey); //已存数据
	        			 long pv = Long.parseLong(mapElement.get("cf:pv").toString());
	        			 long uv = Long.parseLong(mapElement.get("cf:uv").toString());
	        			 long newuv = Long.parseLong(mapElement.get("cf:newuv").toString());
	
	
	        			 if(hBaseMap.containsKey("cf:pv")){
	        				 pv = pv + Long.parseLong(hBaseMap.get("cf:pv").toString());
	        			 }
	
	        			 if(hBaseMap.containsKey("cf:uv")){
	        				 uv = uv + Long.parseLong(hBaseMap.get("cf:uv").toString());
	        			 }
	
	
	        			 if(hBaseMap.containsKey("cf:newuv")){
	        				 newuv = newuv + Long.parseLong(hBaseMap.get("cf:newuv").toString());
	        			 }
	
	
	
	        			 mapElement.put("cf:pv", pv);
	        			 mapElement.put("cf:uv", uv);
	        			 mapElement.put("cf:newuv", newuv);
	
	        			 resultMap.put(newKey, mapElement);
	
	    			 }
	        		 else
	        		 {
	        			 @SuppressWarnings("unchecked")
						Map<String, Object> tmpMap=(Map<String, Object>) entry.getValue();
	        			 tmpMap.put("rowKey", newKey);
	        			 resultMap.put(newKey,tmpMap);
	        		 }
                 }else {
                	 if(resultMap.containsKey(rowKey))
	        		 {
	        			 Map<String,Object> mapElement = resultMap.get(rowKey); //已存数据
	        			 long pv = Long.parseLong(mapElement.get("cf:pv").toString());
	        			 long uv = Long.parseLong(mapElement.get("cf:uv").toString());
	        			 long newuv = Long.parseLong(mapElement.get("cf:newuv").toString());
	
	
	        			 if(hBaseMap.containsKey("cf:pv")){
	        				 pv = pv + Long.parseLong(hBaseMap.get("cf:pv").toString());
	        			 }
	
	        			 if(hBaseMap.containsKey("cf:uv")){
	        				 uv = uv + Long.parseLong(hBaseMap.get("cf:uv").toString());
	        			 }
	
	
	        			 if(hBaseMap.containsKey("cf:newuv")){
	        				 newuv = newuv + Long.parseLong(hBaseMap.get("cf:newuv").toString());
	        			 }
	
	
	        			 mapElement.put("cf:pv", pv);
	        			 mapElement.put("cf:uv", uv);
	        			 mapElement.put("cf:newuv", newuv);
	        			 if(!hBaseMap.containsKey("cf:sdate"))
	        			 {
	        				 mapElement.put("cf:sdate", sdate);
	        			 }
	        			 resultMap.put(rowKey, mapElement);
	
	    			 }
	        		 else
	        		 {
	        			 @SuppressWarnings("unchecked")
						Map<String, Object> tmpMap=(Map<String, Object>) entry.getValue();
	        			 tmpMap.put("rowKey", rowKey);
	        			 tmpMap.put("sdate", sdate);
	        			 resultMap.put(rowKey,tmpMap);
	        		 }
				}
        	}

    	}
    	return resultMap;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TrackURLToPVUVVo> DataInJsonForTrack(
			Map<String, Map<String, Object>> dataMap) throws Exception {
		// TODO Auto-generated method stub
		List<TrackURLToPVUVVo> resultlist = new ArrayList<TrackURLToPVUVVo>();
		boolean statflag=false;
		if (dataMap != null && !dataMap.isEmpty()) {
			List<Map<String, Object>> rsList = ConvertMapToList(dataMap);
			for (int i = 0; i < rsList.size(); i++) {
				String[] tempString=String.valueOf(rsList.get(i)).replace("}", "").split(",");
				TrackURLToPVUVVo tempPvUvVo=new TrackURLToPVUVVo();
				if(tempString.length>=14)
				{   
					
					tempPvUvVo.setPage(tempString[9].split("=")[1]);
					tempPvUvVo.setArea(tempString[6].split("=")[1]);
					tempPvUvVo.setCate1(tempString[8].split("=")[1]);
					tempPvUvVo.setCate2(tempString[7].split("=")[1]);
					tempPvUvVo.setUrl_pv(tempString[4].split("=")[1]);
					
					if(tempString.length>15)
					{	
					  tempPvUvVo.setStatdate(tempString[12].split("=")[1]);
					  tempPvUvVo.setPagetype(tempString[13].split("=")[1]);
					  tempPvUvVo.setUrl_uv(tempString[14].split("=")[1]);
					  tempPvUvVo.setNewuv(tempString[15].split("=")[1]);
					  statflag=true;
					}else {
					  tempPvUvVo.setPagetype(tempString[12].split("=")[1]);
					  tempPvUvVo.setUrl_uv(tempString[13].split("=")[1]);
					  tempPvUvVo.setNewuv(tempString[14].split("=")[1]);
					}
					resultlist.add(tempPvUvVo);
				}
			}
		}
		if(statflag)
		{
			Collections.sort(resultlist);  
		}
		return resultlist;
	}

/*	@Override
	public Map<String, Map<String, Object>> QueryForListByPrefixForClickEvent(//上版入口
			List<String> rowKeyList, String tableName) throws Exception {
		// TODO Auto-generated method stub
		Map <String,Map<String,Object>> resultMap=new HashMap<String,Map<String,Object>>();
    	Map<String, Map<String,Object>> hBaseResult=new HashMap<String,Map<String,Object>>();
    	String rowKey;
		String newKey;
    	for(String rowKeyPrefix:rowKeyList){    	
    		hBaseResult = HbaseUtil.getDataByKeyPrefix(tableName,rowKeyPrefix);
    		for(Entry entry:hBaseResult.entrySet()){
    			Map<String,Object> hBaseMap=(Map<String,Object>)entry.getValue();
        		 rowKey=String.valueOf(entry.getKey());
        		 String keys[]=rowKey.split("\\|");
        		 if(rowKeyPrefix.split("\\|").length>3){
        			 String pageSign=keys[2];
        			 String page=keys[3];
        			 int cityid=Integer.parseInt(keys[4]);
        			 int cateid=Integer.parseInt(keys[5]);
        			 String source=keys[6];
        			 DisplayCate cate=displaycateservice.getOneById(cateid);
        			 DisplayLocal local=displaylocalservice.getOneById(cityid);
        			 hBaseMap.put("cf:cityName",local==null?"--": local.getLocalName());
        			 hBaseMap.put("cf:cateName",cate==null?"--":cate.getCateName());
        			 hBaseMap.put("cf:page", page);
        			 hBaseMap.put("cf:pageSign", pageSign);
        			 hBaseMap.put("cf:source", source);
        		 }else{
        			 String source=keys[2];
        			 String pageSign=keys[3];
        			 String page=keys[4];
        			 int cityid=Integer.parseInt(keys[5]);
        			 int cateid=Integer.parseInt(keys[6]);
        			 DisplayCate cate=displaycateservice.getOneById(cateid);
        			 DisplayLocal local=displaylocalservice.getOneById(cityid);
        			 hBaseMap.put("cf:cityName",local==null?"--": local.getLocalName());
        			 hBaseMap.put("cf:cateName",cate==null?"--":cate.getCateName());
        			 hBaseMap.put("cf:page", page.equals("0")?"--":page);
        			 hBaseMap.put("cf:pageSign",pageSign.equals("0")?"--": pageSign);
        			 hBaseMap.put("cf:source", source);
        		 }
        		 newKey=rowKey.substring(rowKey.indexOf("|", 0)+1);
        		 newKey=newKey.substring(newKey.indexOf("|", 0)+1);
        		 if(resultMap.containsKey(newKey)){
        			 Map<String,Object> mapElement = resultMap.get(newKey); //已存数据
        			 long pv = Long.parseLong(mapElement.get("cf:pv").toString());
        			 long uv = Long.parseLong(mapElement.get("cf:uv").toString());       			         			 
        			 if(hBaseMap.containsKey("cf:pv")){
        				 pv = pv + Long.parseLong(hBaseMap.get("cf:pv").toString());
        			 }        			 
        			 if(hBaseMap.containsKey("cf:uv")){
        				 uv = uv + Long.parseLong(hBaseMap.get("cf:uv").toString());
        			 }        			 
        			 mapElement.put("cf:pv", pv);
        			 mapElement.put("cf:uv", uv);
        		 }else{
        			 Map<String, Object> tmpMap=(Map<String, Object>) entry.getValue();
        			 tmpMap.put("rowKey", newKey);
        			 resultMap.put(newKey,tmpMap); 
        		 }
    		}
    	}
    	
    	//结果集不为空  则累加出PV和UV
    	if(!resultMap.isEmpty() && resultMap.size() > 0){
    		long totalPV = 0;
    		long totaluv = 0;
    		for(Iterator<String> it = resultMap.keySet().iterator(); it.hasNext();){
    			String key = it.next();
    			Map<String,Object> valueMap = resultMap.get(key);
    			totalPV = totalPV + Long.valueOf(valueMap.get("cf:pv").toString());
    			totaluv = totaluv + Long.valueOf(valueMap.get("cf:uv").toString());
    		}
    		Map<String,Object> totalValueMap=new HashMap<String, Object>();
    		totalValueMap.put("cf:source", "总计");
    		totalValueMap.put("cf:cityName","全部");
    		totalValueMap.put("cf:cateName","全部");
    		totalValueMap.put("cf:page", "全部");
    		totalValueMap.put("cf:pageSign", "全部");
    		totalValueMap.put("rowKey", "总计");
    		totalValueMap.put("cf:pv", totalPV);
    		totalValueMap.put("cf:uv", totaluv);
    		resultMap.put("all", totalValueMap);
    	}
    
    	return resultMap;
		
	}*/

	/*@Override
	public List<ClickEventsSourceVo> DataInJsonForClickEvents(
			Map<String, Map<String, Object>> dataMap) throws Exception {
		// TODO Auto-generated method stub
//		{shouye_jjc_5|home|0|0|0|={cf:source=shouye_jjc_5, cf:page=--, cf:pageSign=home, rowKey=shouye_jjc_5|home|0|0|0|, cf:cateName=--, cf:cityName=--, cf:pv=70, cf:uv=65},
		List<ClickEventsSourceVo> resultlist = new ArrayList<ClickEventsSourceVo>();
		if (dataMap != null && !dataMap.isEmpty()) {
			List<Map<String, Object>> rsList = ConvertMapToList(dataMap);
			for (int i = 0; i < rsList.size(); i++) {
				String[] tempString=String.valueOf(rsList.get(i)).replace("}", "").split(",");
				ClickEventsSourceVo tempPvUvVo=new ClickEventsSourceVo();
				if(tempString.length>=8)
				{   tempPvUvVo.setSourcetype(tempString[0].split("=")[1]);
				    tempPvUvVo.setPage(tempString[1].split("=")[1]);
					tempPvUvVo.setPagetype(tempString[2].split("=")[1]);
					tempPvUvVo.setCate(tempString[4].split("=")[1]);
					tempPvUvVo.setCity(tempString[5].split("=")[1]);
					tempPvUvVo.setUrl_pv(tempString[6].split("=")[1]);
					tempPvUvVo.setUrl_uv(tempString[7].split("=")[1]);
					resultlist.add(tempPvUvVo);
				}
			}
		}
		return resultlist;
	}*/

	@Override
	public List<ClickCityVo> QueryForListByPrefixForClickCity(
			List<String> rowKeyList, HashSet<String> wdzbSet, int limit)
			throws Exception {
		// TODO Auto-generated method stub
		//获取查询参数
		//获取实际HBase表名
		String hbaseTableName = "ZhClickEventType";
		String splitTag = "::";
	    
	    //最终输出结果集
	    List<ClickCityVo> allResult=new ArrayList<ClickCityVo>();
    	
    	//获取结果
    	int curNum = 0;
    	for(String rowKeyPrefix:rowKeyList)
    	{
    		//判断结构数已够
    		if(curNum >= limit)
    		{
    			break;
    		}
    		String nextKey = "";
    		//当前前缀结果未完
    		while(nextKey!= null && curNum < limit)
    		{
    			//每次HBase查询中间结果集
    	    	Map<String, List<Map<String,Object>> > hbaseResult = new HashMap<String,List<Map<String,Object>> >();
    			hbaseResult = HbaseUtil.getNext100ByKeyPrefix(hbaseTableName, rowKeyPrefix, nextKey);
        		//判断当前单个前缀已查完
        		if(hbaseResult == null || hbaseResult.isEmpty())
        		{
        			nextKey = null;
        			break;
        		}
        		for(@SuppressWarnings("rawtypes") Entry entry:hbaseResult.entrySet())
        		{
        			nextKey=String.valueOf(entry.getKey());
        			@SuppressWarnings("unchecked")
					List<Map<String,Object>> kvInList=(List<Map<String,Object>>)entry.getValue();
        			
        			//遍历这次查询的结果集
        			for(Map<String,Object> oneMap:kvInList)
        			{
        				
        				ClickCityVo oneResult = new ClickCityVo();
        				//判断结构数已够
        	    		if(curNum >= limit)
        	    		{
        	    			break;
        	    		}
        				String rowkey = (String)oneMap.get("rowKey");
        				if(rowkey!=null)
        				{
	        				String date="";
	        				String click="";
	        				String city="";
	        				try {
	        					String [] keyArr = rowkey.split(splitTag);
	        					    date = keyArr[0];
	            				oneResult.setStatdate(date);
	            				//String platform = keyArr[1];
	            				    click = keyArr[2];
	            				oneResult.setClicktag(click);
	            				    city = keyArr[3];
							} catch (Exception e) {
								// TODO: handle exception
								System.out.println(oneMap);
							}
	        				
	        				
	        				if((wdzbSet.contains("cityCombine") && city.equals("ALL")) || (!wdzbSet.contains("cityCombine") && !city.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setCity(city);
	        				}
	        				
	        				String pv = (String)oneMap.get("cf:pv");
	        				if(wdzbSet.contains("clickcountIndex"))
	        				{
	        					oneResult.setClickcount(pv);
	        				}
	        				String uv = (String)oneMap.get("cf:uv");
	        				if(wdzbSet.contains("clickpeopleIndex"))
	        				{
	        					oneResult.setClickpeople(uv);
	        				}
	        				
	        				//算一条返回
	        				curNum ++;
	        				allResult.add(oneResult);
        				}
        			}    			
            	}    
    		}   		
    	}
    
    	return allResult;
	}

	@Override
	public List<ClickEventsSourceVo> QueryForListByPrefixForClickEvent(String hbaseTableName,
			List<String> rowKeyList, HashSet<String> wdzbSet, int limit)
			throws Exception {
		// TODO Auto-generated method stub
		String splitTag = "::";  
	    //最终输出结果集
	    List<ClickEventsSourceVo> allResult = new ArrayList<ClickEventsSourceVo>();
    	
    	//获取结果
    	int curNum = 0;
    	for(String rowKeyPrefix:rowKeyList)
    	{
    		//判断结构数已够
    		if(curNum >= limit)
    		{
    			break;
    		}
    		String nextKey = "";
    		//当前前缀结果未完
    		while(nextKey!= null && curNum < limit)
    		{
    			//每次HBase查询中间结果集
    	    	Map<String, List<Map<String,Object>> > hbaseResult = new HashMap<String,List<Map<String,Object>> >();
    	    	
    			hbaseResult = HbaseUtil.getNext100ByKeyPrefix(hbaseTableName, rowKeyPrefix, nextKey);
        		//判断当前单个前缀已查完
        		if(hbaseResult == null || hbaseResult.isEmpty())
        		{
        			nextKey = null;
        			break;
        		}
        		for(@SuppressWarnings("rawtypes") Entry entry:hbaseResult.entrySet())
        		{
        			nextKey=String.valueOf(entry.getKey());
        			@SuppressWarnings("unchecked")
					List<Map<String,Object>> kvInList=(List<Map<String,Object>>)entry.getValue();
        			
        			//遍历这次查询的结果集
        			for(Map<String,Object> oneMap:kvInList)
        			{
        				ClickEventsSourceVo oneResult = new ClickEventsSourceVo();
        				//判断结果数已够
        	    		if(curNum >= limit)
        	    		{
        	    			break;
        	    		}
        				String rowkey = (String)oneMap.get("rowKey");
        				if(rowkey!=null)
        				{
	        				String [] keyArr = rowkey.split(splitTag);
	        				String date = keyArr[0];
	        				oneResult.setStatdate(date);
	        				//String platform = keyArr[1];
	        				String source = keyArr[2];
	        				oneResult.setSourcename(source);
	        				String pagetype = keyArr[3];//维度里面包含pagetype，并且pagetype不等于all，若不包含则只算等于all的（在算其他的时候）
	        				if((wdzbSet.contains("PageTypeCombine") && pagetype.equals("ALL")) || (!wdzbSet.contains("PageTypeCombine") && !pagetype.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setPagetype(pagetype);
	        				}
	        				String twopage = keyArr[4];
	        				if((wdzbSet.contains("TwoPageTypeCombine") && twopage.equals("ALL")) || (!wdzbSet.contains("TwoPageTypeCombine") && !twopage.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setTwopagetype(twopage);
	        				}
	        				String cate1 = keyArr[5];
	        				if((wdzbSet.contains("cateOneCombine") && cate1.equals("ALL")) || (!wdzbSet.contains("cateOneCombine") && !cate1.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setCate1(cate1);
	        				}
	        				String cate2 = keyArr[6];
	        				if((wdzbSet.contains("cateTwoCombine") && cate2.equals("ALL")) || (!wdzbSet.contains("cateTwoCombine") && !cate2.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setCate2(cate2);
	        				}
	        				String pv = (String)oneMap.get("cf:pv");
	        				if(wdzbSet.contains("PvIndex"))
	        				{
	        					oneResult.setPv(pv);
	        				}
	        				String uv = (String)oneMap.get("cf:uv");
	        				if(wdzbSet.contains("UvIndex"))
	        				{
	        					oneResult.setUv(uv);
	        				}
	        				
	        				//算一条返回
	        				curNum ++;
	        				allResult.add(oneResult);
        				}
        			}
        			
        			
            	} 

    
    		}
    		
    		
    	}
    
    	return allResult;

	}

	@Override
	public List<ClickEventsSourceVo> QueryForListByPrefixForSourceAll(
			String hbaseTableName, List<String> rowKeyList, HashSet<String> wdzbSet,
			int limit) throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		String splitTag = "::";  
	    //最终输出结果集
	    List<ClickEventsSourceVo> allResult = new ArrayList<ClickEventsSourceVo>();
    	
    	//获取结果
    	int curNum = 0;
    	for(String rowKeyPrefix:rowKeyList)
    	{
    		//判断结构数已够
    		if(curNum >= limit)
    		{
    			break;
    		}
    		String nextKey = "";
    		//当前前缀结果未完
    		while(nextKey!= null && curNum < limit)
    		{
    			//每次HBase查询中间结果集
    	    	Map<String, List<Map<String,Object>> > hbaseResult = new HashMap<String,List<Map<String,Object>> >();
    	    	
    			hbaseResult = HbaseUtil.getNext100ByKeyPrefix(hbaseTableName, rowKeyPrefix, nextKey);
        		//判断当前单个前缀已查完
        		if(hbaseResult == null || hbaseResult.isEmpty())
        		{
        			nextKey = null;
        			break;
        		}
        		for(@SuppressWarnings("rawtypes") Entry entry:hbaseResult.entrySet())
        		{
        			nextKey=String.valueOf(entry.getKey());
        			@SuppressWarnings("unchecked")
					List<Map<String,Object>> kvInList=(List<Map<String,Object>>)entry.getValue();
        			
        			//遍历这次查询的结果集
        			for(Map<String,Object> oneMap:kvInList)
        			{
        				ClickEventsSourceVo oneResult = new ClickEventsSourceVo();
        				//判断结果数已够
        	    		if(curNum >= limit)
        	    		{
        	    			break;
        	    		}
        				String rowkey = (String)oneMap.get("rowKey");
        				if(rowkey!=null)
        				{
	        				String [] keyArr = rowkey.split(splitTag);
	        				String date = keyArr[1];
	        				oneResult.setStatdate(date);
	        				//String platform = keyArr[1];
	        				String source = keyArr[3];
	        				oneResult.setSourcename(source);
	        				String pagetype = keyArr[4];
	        				if((wdzbSet.contains("PageTypeCombine") && pagetype.equals("ALL")) || (!wdzbSet.contains("PageTypeCombine") && !pagetype.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setPagetype(pagetype);
	        				}
	        				String twopage = keyArr[5];
	        				if((wdzbSet.contains("TwoPageTypeCombine") && twopage.equals("ALL")) || (!wdzbSet.contains("TwoPageTypeCombine") && !twopage.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setTwopagetype(twopage);
	        				}
	        				
	        				String cate1 = keyArr[6];
	        				if((wdzbSet.contains("cateOneCombine") && cate1.equals("ALL")) || (!wdzbSet.contains("cateOneCombine") && !cate1.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setCate1(cate1);
	        				}
	        				String cate2 = keyArr[7];
	        				if((wdzbSet.contains("cateTwoCombine") && cate2.equals("ALL")) || (!wdzbSet.contains("cateTwoCombine") && !cate2.equals("ALL")))
	        				{
	        					continue;
	        				}
	        				else
	        				{
	        					oneResult.setCate2(cate2);
	        				}
	        				String pv = (String)oneMap.get("cf:pv");
	        				if(wdzbSet.contains("PvIndex"))
	        				{
	        					oneResult.setPv(pv);
	        				}
	        				String uv = (String)oneMap.get("cf:uv");
	        				if(wdzbSet.contains("UvIndex"))
	        				{
	        					oneResult.setUv(uv);
	        				}
	        				
	        				//算一条返回
	        				curNum ++;
	        				allResult.add(oneResult);
        				}
        			}
        			
        			
            	} 

    
    		}
    		
    		
    	}
    
    	return allResult;
	}

	@Override
	public List<ConflictDelectVo> QueryForListByPrefixForConflictDelect(
			String rowKey, int limit) throws Exception {
		// TODO Auto-generated method stub
		String splitTag = "::";  
	    //最终输出结果集
	    List<ConflictDelectVo> Result = new ArrayList<ConflictDelectVo>();
    	//获取实际HBase表名
		String hbaseTableName = "LConflictDelect";
		Map<String, List<Map<String,Object>> > hbaseResult = new HashMap<String,List<Map<String,Object>> >();
		hbaseResult = HbaseUtil.getNext100ByKeyPrefix(hbaseTableName, rowKey, "");
		if(hbaseResult==null)
			return null;
		for(@SuppressWarnings("rawtypes") Entry entry:hbaseResult.entrySet())
		{
			@SuppressWarnings("unchecked")
			List<Map<String,Object>> kvInList=(List<Map<String,Object>>)entry.getValue();
			
			//遍历这次查询的结果集{cf:startdate=20151101, rowKey=58PC::1::home_0_3::-, cf:update=20151126}
			for(Map<String,Object> oneMap:kvInList)
			{
				ConflictDelectVo oneResult = new ConflictDelectVo();
				String rowkey = (String)oneMap.get("rowKey");
				String startdate=(String)oneMap.get("cf:startdate");
				oneResult.setStartdate(startdate);
				String [] keyArr = rowkey.split(splitTag);
				if("1".equals(keyArr[1].trim()))
				{
					oneResult.setClicktag(keyArr[2]);
				}else if("2".equals(keyArr[1].trim()))
				{
					oneResult.setSource(keyArr[2]);
				}else if("3".equals(keyArr[1].trim()))
				{
					oneResult.setPagetype(keyArr[2]);
					oneResult.setPage(keyArr[3]);
				}else if("4".equals(keyArr[1].trim()))
				{
					oneResult.setPage(keyArr[2]);
					oneResult.setPagetype(keyArr[3]);
				}
				Result.add(oneResult);
			}
		}
		
		return Result;
	}

}
